package com.uh.calculation.inter.bathImp;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonArray;
import com.uh.calculation.batchOperator.*;
import com.uh.calculation.bean.OperatorVal;
import com.uh.calculation.bean.SqlWriteBean;
import com.uh.calculation.bean.WarnJiSuanBean;
import com.uh.calculation.inter.Warn;
import com.uh.calculation.operator.*;
import com.uh.calculation.util.GetOrganization;
import com.uh.calculation.util.OperatorXmlUtil;
import com.uh.microservice.core.dao.JdbcDao;
import com.uh.microservice.core.util.ThreadPool;
import com.uh.summary.enums.RuleEnum;
import com.uh.summary.mapper.BathMapper;
import com.uh.summary.mapper.WarnEnterpriseRecordMapper;
import com.uh.summary.serviceImpl.ServerThread;
import org.apache.ibatis.annotations.Param;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class BatchMaoyiImp implements Warn {

    private int start; private int offset;


    //@Resource
    private WarnEnterpriseRecordMapper warnEnterpriseRecordMapper;
    private JdbcDao jdbcDao;

    private BathMapper  bathMapper;
    /**
     * 业务变量配置
     *
     * @param warnJiSuan
     * @return
     */
    //////////////////////////////////////////////////////////////修改
    //修改频次
    private String xgpc = "";

    //1-本月 营业收入修改比例超过15%，计预警因子50。
    //年报营业收入修改比例超过15%，计预警因子50。（此条仅用于4月年报出来时，但一直触发到该企业被核查或检查为止）
    private String  xgfd="[{dimension:\"A\",field:\"$NEW_VALUE;$NEW_VALUE#m\",formulaBeans:[{formula:\"($NEW_VALUE@$NEW_VALUE#m)/$NEW_VALUE#m>0.15|50\"}]}]";

    private String  xgcs="[{dimension:\"A\",field:\"$NEW_VALUE\",formulaBeans:[{formula:\"$NEW_VALUE>3|50\"}]}]";




    //////////////////////////////////////////////////////////////异常
    //运营状态  目前无准确字段
    private String yxzt = "[{dimension:\"B\",field:\"201-1_M62\",formulaBeans:[{formula:\"201-1_M62=2|20\"},{formula:\"201-1_M62=4|20\"},{formula:\"201-1_M62=5|20\"},{formula:\"201-1_M62=6|20\"},{formula:\"201-1_M62=7|20\"}]}]";
    //被举报
    private String bjb = "";
    //填表人变动
    private String tbr = "";
    //贸易-批零
    //private String pl = "[{\"dimension\":\"B\",\"field\":\"E104-1_M11;E104-1_M9\",\"formulaBeans\":[{\"formula\":\"E104-1_M9-E104-1_M11>0&E104-1_M9<24000|50\"},{\"formula\":\"(E104-1_M11-E104-1_M9)>0&E104-1_M11<5100|50\"},{\"formula\":\"E204-1_M9<5100|50\"},{\"formula\":\"E204-1_M11<24000|50\"}]}]";
    private String pl = "[{\"dimension\":\"B\",\"field\":\"E104-1_M11;E104-1_M9\",\"formulaBeans\":[{\"formula\":\"E104-1_M9-E104-1_M11>0&E104-1_M9<24000|50\"}]}]";
    //贸易-住餐
    private String zc = "[{dimension:\"B\",field:\"S104-1_M1\",formulaBeans:[{formula:\"E104-1_M<2400|50\"}]}]";


    //////////////////////////////////////////////////////////////增速
    //本月销售额（营业额）(e204-1 M12)同比(去年e204-1 M14)/环比(上个月e204-1 M12)增速增长或下降为30%，计150分，每超过30%一个百分点，加5分。
    private String zs1 = "[{dimension:\"D\",field:\"E204-1_M12;E204-1_M14\",formulaBeans:[{formula:\"((E204-1_M12@E204-1_M14)/E204-1_M14)>0.3&((((E204-1_M12@E204-1_M14)/E204-1_M14)-0.3)*100)*5+150>=300|300\"},{formula:\"((E204-1_M12@E204-1_M14)/E204-1_M14)>0.3&((((E204-1_M12@E204-1_M14)/E204-1_M14)-0.3)*100)*5+150<300|((((E204-1_M12@E204-1_M14)/E204-1_M14)-0.3)*100)*5+150\"}]}]";
    //每季营业收入(e203 M26)增速同比(e203 M27)增长超20%，计50分，每超过20%一个百分点，加2分。（该条只会在每季季报出来后才会触发，一旦触发，持续到该企业被核查或检查，企业被核查或检查后消失）
    private String zs2 = "[{dimension:\"D\",field:\"E203_M26;E203_M27\",formulaBeans:[{formula:\"((E203_M26@E203_M27)/E203_M27)>0.2&((((E203_M26@E203_M27)/E203_M27)-0.2)*100)*2+50>=100|100\"},{formula:\"((E203_M26@E203_M27)/E203_M27)>0.2&((((E203_M26@E203_M27)/E203_M27)-0.2)*100)*2+50<100|((((E203_M26@E203_M27)/E203_M27)-0.2)*100)*2+50\"}]}]";
    //////////////////////////////////////////////////////////////匹配

    //每季末的1-本月销售额(e204-1 M13)/季报上的营业收入(e203 M26)为115%时，计100分，每比115%大一个百分点，加2分。为85%时，计100分，每小85%一个百分点，加2分。
    private String pp1 = "[{dimension:\"E\",field:\"E204-1_M13;E203_M26\",formulaBeans:[{formula:\"E204-1_M13/E203_M26>1.15&((((E204-1_M13/E203_M26)-1.15)*100)*2)+100>=200|200\"},{formula:\"E204-1_M13/E203_M26>1.15&((((E204-1_M13/E203_M26)-1.15)*100)*2)+100<200|((((E204-1_M13/E203_M26)-1.15)*100)*2)+100\"},{formula:\"E204-1_M13/E203_M26<0.85&((((E204-1_M13/E203_M26)@0.85)*100)*2)+100>=200|200\"},{formula:\"E204-1_M13/E203_M26<0.85&((((E204-1_M13/E203_M26)@0.85)*100)*2)+100<200|((((E204-1_M13/E203_M26)@0.85)*100)*2)+100\"}]}]";


    //////////////////////////////////////////////////////////////调整

    //本月商品零售额(e204-1 M20)+上个月的1-本月商品零售额(e204-1 M21)与1-本月商品零售额(e204-1 M21)的差异率为20%，计50分。每超过20%一个百分点，多加1分。如差异率为21%，分数51；增速为25%，分数55分，增速30%，分数60分；增速11.5%，分数为51.5分
    private String tz2 = "[{\"dimension\":\"C\",\"field\":\"E204-1_M20;E204-1_M21#m;E204-1_M21;\",\"formulaBeans\":[{\"formula\":\"(((E204-1_M20+E204-1_M21#m)/E204-1_M21)-1)=0.2|50\"},{\"formula\":\"(((E204-1_M20+E204-1_M21#m)/E204-1_M21)-1)>0.2|((((E204-1_M20+E204-1_M21#m)/E204-1_M21)-1)-0.2)/0.01*1+50\"}]}]";
    //上一年度12个月的本月商品销售额(e204-1 M12)相加与年报商品销售额(e104-1 M6)的差异率超过20%，计50分。每超过20%一个百分点，多加2分。（一年以内这一条在4月份年报出来的时候使用，然后一直保留这家企业被核查或检查，一旦哪个月被核查或检查，这条消失）
    private String tz3 = "[{\"dimension\":\"C\",\"field\":\"E204-1_M12;E104-1_M6;\",\"formulaBeans\":[{\"formula\":\"E204-1_M12/E104-1_M6=1.2|50\"},{\"formula\":\"E204-1_M12/E104-1_M6>1.2|((((E204-1_M12/E104-1_M6)-1.2)/0.01)*2)+50\"}]}]";
    //上一年度12个月的本月零售额(e204-1 M20)相加与年报零售额(e104-1 M11)的差异率超过20%，计50分。每超过20%一个百分点，多加2分。（一年以内这一条在4月份年报出来的时候使用，然后一直保留这家企业被核查或检查，一旦哪个月被核查或检查，这条消失）
    private String tz4 = "[{\"dimension\":\"C\",\"field\":\"E204-1_M20;E104-1_M11;\",\"formulaBeans\":[{\"formula\":\"E204-1_M20/E104-1_M11=1.2|50\"},{\"formula\":\"E204-1_M20/E104-1_M11>1.2|((((E204-1_M20/E104-1_M11)-1.2)/0.01)*2)+50\"}]}]";
    //本月商品销售额(e204-1 M12)+上个月的1-本月商品销售额(e204-1 M13)与1-本月商品销售额(e204-1 M13)的差异率为20%，计50分。每超过20%一个百分点，多加1分。如差异率为21%，分数51；增速为25%，分数55分，增速30%，分数60分；增速11.5%，分数为51.5分。
    private String tz1 = "[{\"dimension\":\"C\",\"field\":\"E204-1_M12;E204-1_M13#m;E204-1_M13;\",\"formulaBeans\":[{\"formula\":\"(((E204-1_M12+E204-1_M13#m)/E204-1_M13)-1)=0.2|50\"},{\"formula\":\"(((E204-1_M12+E204-1_M13#m)/E204-1_M13)-1)>0.2|((((E204-1_M12+E204-1_M13#m)/E204-1_M13)-1)-0.2)/0.01*1+50\"}]}]";


    public WarnJiSuanBean warnJiSuan = null;


    //通用表
    List<Map<String, Object>> E203ls = null;
    List<Map<String, Object>> E204ls = null;

    List<Map<String, Object>> TZSum1 = null;
    List<Map<String, Object>> TZSum2 = null;
    List<Map<String, Object>> E104ls = null;
    List<Map<String, Object>> S104ls = null;
    List<Map<String, Object>> S204_1ls = null;
    List<Map<String, Object>> E204_1ls = null;
    List<Map<String, Object>> F201_1ls = null;

    List<Map<String, Object>> HJ_ls = null;
    List<Map<String, Object>> HJN_ls = null;






    @Override
    public Integer jisuan(WarnJiSuanBean warnJiSuan) {
        this.warnJiSuan = warnJiSuan;
        this.jdbcDao = warnJiSuan.getJdbcDao();
        this.bathMapper=warnJiSuan.getBathMapper();
        GetOrganization getOrganization = new GetOrganization(jdbcDao);
        List<OperatorVal> list = JSONObject.parseArray(xgfd, OperatorVal.class);
        try {
            E204ls = bathMapper.getElist(warnJiSuan.getSt(), warnJiSuan.getOffset());

            System.out.println("总行行数："+warnJiSuan.getTotalNum()+" 当前处理行："+warnJiSuan.getSt()+"-"+warnJiSuan.getOffset());

            warnJiSuan.setIsSelect(true);
            for(Map<String, Object> m :E204ls) {
                try {
                    SqlWriteBean sqlWriteBean  =  new SqlWriteBean();
                    JSONArray jsonArray=null;
                    JSONArray jsonArray203=null;
                    JSONArray jsonArray104=null;
                    JSONArray jsonArrayHJ_ls=null;
                    JSONArray jsonArrayHJN_ls=null;
                    JSONArray jsonArray201 = null;
                    if( m.get("et204Res")!=null){
                        String topicResponsibles = m.get("et204Res").toString();
                        jsonArray = JSONArray.parseArray("[" + topicResponsibles + "]");
                    }
                    if( m.get("et203Res")!=null) {
                        String et203Res = m.get("et203Res").toString();
                        jsonArray203 = JSONArray.parseArray("[" + et203Res + "]");
                    }
                    if (m.get("et201Res") != null) {
                        String et201Res = m.get("et201Res").toString();
                        jsonArray201 = JSONArray.parseArray("[" + et201Res + "]");
                    }
                    if( m.get("et104Res")!=null) {
                        String et104Res = m.get("et104Res").toString();
                        jsonArray104 = JSONArray.parseArray("[" + et104Res + "]");
                    }
                    if( m.get("ethjRes")!=null) {
                        String ethjRes = m.get("ethjRes").toString();
                        jsonArrayHJ_ls = JSONArray.parseArray("[" + ethjRes + "]");
                    }
                    if( m.get("ethjnRes")!=null) {
                        String ethjnRes = m.get("ethjnRes").toString();
                        jsonArrayHJN_ls = JSONArray.parseArray("[" + ethjnRes + "]");
                    }

                    sqlWriteBean.setJsonArray201(jsonArray201);
                    //System.out.println(jsonArray.toString());
                    //JSONArray json = JSONArray.fromObject(topicResponsibles );
                    //启动同比增长算子
                    ZsBathOperator zsOperator = new ZsBathOperator(jdbcDao, warnJiSuan,sqlWriteBean);
                    list =  list = OperatorXmlUtil.readXml("maoyi_zs1");
                    //JSONObject.parseArray(zs1, OperatorVal.class);
                    sqlWriteBean.setIs_hc(false);
                    sqlWriteBean.setWarnType(RuleEnum.TRADE_GROW_FACTOR_DATA.getCode());//风险因子类型
                    zsOperator.jisuan(jsonArray, list);

                    list = JSONObject.parseArray(zs2, OperatorVal.class);
                    sqlWriteBean.setIs_hc(false);
                    sqlWriteBean.setWarnType(RuleEnum.TRADE_GROW_FACTOR_DATA1.getCode());//风险因子类型
                    zsOperator.jisuan(jsonArray, list);


                    Tz1BathOperator tz1Operator = new Tz1BathOperator(jdbcDao, warnJiSuan,sqlWriteBean);
                    //调整1
                    list = JSONObject.parseArray(tz1, OperatorVal.class);
                    sqlWriteBean.setIs_hc(false);
                    sqlWriteBean.setWarnType(RuleEnum.TRADE_ADJUSTMENT_FACTOR_MONTH_DATA1.getCode());//风险因子类型
                    tz1Operator.jisuan(jsonArray, list);



                    SyBathOperator syOperator = new SyBathOperator(jdbcDao, warnJiSuan,sqlWriteBean);

                    list = JSONObject.parseArray(tz2, OperatorVal.class);
                    sqlWriteBean.setIs_hc(false);
                    sqlWriteBean.setWarnType(RuleEnum.TRADE_ADJUSTMENT_FACTOR_MONTH_DATA2.getCode());//风险因子类型
                    syOperator.jisuan(jsonArray, list);


                    JtYBathOperator jtYOperator = new JtYBathOperator(jdbcDao, warnJiSuan,sqlWriteBean);
                    list = JSONObject.parseArray(pp1, OperatorVal.class);
                    //匹配一
                    sqlWriteBean.setIs_hc(false);
                    sqlWriteBean.setWarnType(RuleEnum.TRADE_MATCH_FACTOR_DATA.getCode());//风险因子类型
                    jtYOperator.jisuan(jsonArray203, jsonArray, list);



                    NbBathOperator nbOperator = new NbBathOperator(jdbcDao, warnJiSuan,sqlWriteBean);
                    list = JSONObject.parseArray(tz3, OperatorVal.class);
                    sqlWriteBean.setIs_hc(true);
                    sqlWriteBean.setWarnType(RuleEnum.TRADE_ADJUSTMENT_FACTOR_YEAR_DATA1.getCode());//风险因子类型
                    nbOperator.jisuan(jsonArray104, jsonArray, list);

                    list = JSONObject.parseArray(tz4, OperatorVal.class);
                    sqlWriteBean.setIs_hc(true);
                    sqlWriteBean.setWarnType(RuleEnum.TRADE_ADJUSTMENT_FACTOR_YEAR_DATA2.getCode());//风险因子类型
                    nbOperator.jisuan(jsonArray104, jsonArray, list);


                    //小升规  贸易-批零
                    //XsgOperator xsgOperator = new XsgOperator(jdbcDao, warnJiSuan);
                    list = JSONObject.parseArray(pl, OperatorVal.class);
                    sqlWriteBean.setIs_hc(false);
                    sqlWriteBean.setWarnType(RuleEnum.TRADE_ABNORMAL_SMALL_FACTOR_DATA1.getCode());//风险因子类型
                    zsOperator.jisuan(jsonArray104, list);


                    //启动统计人员变更计算
                    TjryBathOperator tjryOperator = new TjryBathOperator(jdbcDao, warnJiSuan,sqlWriteBean);
                    sqlWriteBean.setIs_hc(false);
                    sqlWriteBean.setWarnType(RuleEnum.TRADE_ABNORMAL_STATISTICS_FACTOR_DATA.getCode());//风险因子类型
                    //tjryOperator.jisuan(S204_1ls, "M40", "B");
                    tjryOperator.jisuan(jsonArray, "M5", "B");

                    HjBathOperator hjOperator = new HjBathOperator(jdbcDao, warnJiSuan,sqlWriteBean);

                    //修改一
                    sqlWriteBean.setIs_hc(false);
                    sqlWriteBean.setWarnType(RuleEnum.TRADE_EDIT_FREQUENCY_FACTOR_DATA.getCode());//风险因子类型
                    hjOperator.jisuan(jsonArrayHJ_ls, list,"本年;本月;商品销售额;千元");
                    hjOperator.jisuan(jsonArrayHJ_ls, list,"本年;1-本月;商品销售额;千元");
                    hjOperator.jisuan(jsonArrayHJ_ls, list,"本年;本月;零售额;千元");
                    hjOperator.jisuan(jsonArrayHJ_ls, list,"本年;1-本月;零售额;千元");
                    hjOperator.jisuan(jsonArrayHJ_ls, list,"本年;1-本月;零售额;千元");

                    sqlWriteBean.setIs_hc(true);
                    hjOperator.jisuan(jsonArrayHJN_ls, list,"本年;商品销售额;零售额;千元");
                    hjOperator.jisuan(jsonArrayHJN_ls, list,"本年;服务营业额;千元");


                    list = JSONObject.parseArray(xgcs, OperatorVal.class);
                    HjBathNumOperator hjNumOperator = new HjBathNumOperator(jdbcDao, warnJiSuan,sqlWriteBean);
                    sqlWriteBean.setWarnType(RuleEnum.TRADE_EDIT_FREQUENCY_FACTOR_MONTH_DATA.getCode());//风险因子类型
                    sqlWriteBean.setIs_hc(false);
                    hjNumOperator.jisuan(jsonArrayHJ_ls, list,"本年;本月;商品销售额;千元");
                    hjNumOperator.jisuan(jsonArrayHJ_ls, list,"本年;1-本月;商品销售额;千元");
                    hjNumOperator.jisuan(jsonArrayHJ_ls, list,"本年;本月;零售额;千元");
                    hjNumOperator.jisuan(jsonArrayHJ_ls, list,"本年;1-本月;零售额;千元");

                    sqlWriteBean.setIs_hc(true);
                    sqlWriteBean.setWarnType(RuleEnum.TRADE_EDIT_FREQUENCY_FACTOR_YEAR_DATA.getCode());//风险因子类型
                    hjNumOperator.jisuan(jsonArrayHJ_ls, list,"本年;商品销售额;零售额;千元");
                    hjNumOperator.jisuan(jsonArrayHJ_ls, list,"本年;服务营业额;千元");
                    hjNumOperator.jisuan(jsonArrayHJ_ls, list,"本年;商品销售额;批发额;千元");
                    hjNumOperator.jisuan(jsonArrayHJ_ls, list,"本年;商品销售额;批发额;千元");


                    //运营状态
                    YyztBathOperator yyztOperator = new YyztBathOperator(jdbcDao, warnJiSuan,sqlWriteBean);
                    list = JSONObject.parseArray(yxzt, OperatorVal.class);
                    sqlWriteBean.setIs_hc(false);
                    sqlWriteBean.setWarnType(RuleEnum.SERVICE_ABNORMAL_MANAGE_FACTOR_DATA.getCode());//风险因子类型
                    yyztOperator.jisuanYyzt(jsonArray201, list);



                }catch (Exception e){
                    //e.printStackTrace();
                }
            }
        }catch (Exception E){
            // E.printStackTrace();
        }
        //}











        //小升规  贸易-住餐
        /*list = JSONObject.parseArray(zc, OperatorVal.class);
        warnJiSuan.setIs_hc(false);
        warnJiSuan.setWarnType(RuleEnum.TRADE_ABNORMAL_SMALL_FACTOR_DATA2.getCode());//风险因子类型
        xsgOperator.jisuanMaoyiZc(S104ls, list);*/

        //运营状态
        /*YyztOperator yyztOperator = new YyztOperator(jdbcDao, warnJiSuan);
        list = JSONObject.parseArray(yxzt, OperatorVal.class);
        yyztOperator.jisuanYyzt(F201_1ls, list);
        */


        return null;
    }


}
